package com.linewow.xhyy.nihaonew.base;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.canyinghao.candialog.CanDialog;
import com.linewow.xhyy.hrxb.DemoHelper;
import com.linewow.xhyy.nihaonew.R;
import com.linewow.xhyy.nihaonew.application.NiHaoApp;
import com.linewow.xhyy.nihaonew.util.SnackbarUtil;
import com.umeng.analytics.MobclickAgent;

/**
 * Created by LXR on 2016/8/22.
 *
 */
public class BaseFragment extends Fragment {
    private String analysisName;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        analysisName=this.getClass().getCanonicalName();
        View view=initView();
        return view;
    }

    protected void  showProgressBar(){
        getView().findViewById(R.id.base_fragment_prg).setVisibility(View.VISIBLE);
    }
    protected void  dismissProgressBar(){
        getView().findViewById(R.id.base_fragment_prg).setVisibility(View.GONE);
    }

    protected void showNoData(){
        getView().findViewById(R.id.base_fragment_no_data).setVisibility(View.VISIBLE);
    }
    protected void dismissNoData(){
        getView().findViewById(R.id.base_fragment_no_data).setVisibility(View.GONE);
    }



    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();
        initList();
        setUpView();
    }

    protected void setUpView() {

    }

    public void initData() {

    }

    public void initList() {

    }

    public View initView() {
        return null;
    }

    private ProgressDialog dialog;

    public void showLoading() {
        if(getActivity()instanceof BaseFragmentActivity){
            ((BaseFragmentActivity)getActivity()).showLoading();
        }else{
            ((BaseCompatActivity)getActivity()).showLoading();
        }
    }

    public void dismissLoading() {
        if(getActivity()instanceof BaseFragmentActivity){
            ((BaseFragmentActivity)getActivity()).dismissLoading();
        }else{
            ((BaseCompatActivity)getActivity()).dismissLoading();
        }
    }

    protected void hideSoftKeyboard() {

        ((BaseFragmentActivity)getActivity()).hideSoftKeyboard();
    }

    public boolean noLogin(){
        if(NiHaoApp.flag){
            return  false;
        }else{
            DemoHelper.getInstance().onConnectionConflict();
            return true;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(analysisName);
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(analysisName);
    }


    protected void showSnack(String message){
        SnackbarUtil.NihaoSnackbar(BaseFragment.this,message).show();
    }
    protected void showToast(String message){
        Toast.makeText(BaseFragment.this.getActivity(), message, Toast.LENGTH_SHORT).show();
    }
    protected void showCanDialog(String msg){
        new CanDialog.Builder(BaseFragment.this.getActivity())
                .setTitle(getString(R.string.menu_notice))
                .setMessage(msg)
                .setCircularRevealAnimator(CanDialog.CircularRevealStatus.TOP_LEFT)
                .setPositiveButton(getString(R.string.widget_sure),true,null)
                .show();
    }

}
